#encoding:utf-8
#

from Tkinter import *
from dir import *
from command import *
from indexdata import *

class Start:
	def __init__(self):
		 self.root=None
		 self.entry=None
		 self.text=None
		 self.tempdict=None
		 self.tempindex=None
		 
	def initWindow(self):
		 #初始化
		 self.root = Tk()
		 self.root.title('pystart')
		 self.root.protocol("WM_DELETE_WINDOW", self.exitWindow) #窗口绑定关闭事件
 
	def enterFun(self, event=None):
		#回车
		word=self.entry.get()
		if word !='':
			if word[0] != '-':
				if INDEXDATA().isindex(word):
					self.tempindex=word
					self.tempdict = DIR().getapplist(word)
					self.text.delete(1.0,END)
					for i in range(0,len(self.tempdict)):
						self.text.insert(END,'['+str(i)+'] -- '+self.tempdict.keys()[i]+'\n')
					self.text.insert(1.0,'#index:'+word+'\n')
					self.text.pack()
			else:
				command=COMMAND(self.tempdict,self.tempindex)
				msgs=command.send(word)
				if msgs:
					self.text.delete(1.0,END)
					for msg in msgs:
						self.text.insert(END,msg+"\n")
					self.text.pack()
		
		self.entry.delete(0,END)
 
	def exitWindow(self):
		 #退出
		 self.root.quit()
 
	def main(self):
		 #控件加载
		 self.entry=Entry(self.root,width='57')
		 self.entry.bind('<Return>',self.enterFun)
		 self.entry.pack()
		 self.entry.focus()
		 self.text=Text(self.root,width='50')
 
	def run(self):
		 self.initWindow()
		 self.main()
		 self.root.mainloop()
		 
if __name__ == '__main__':
	Start().run()